/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.grouplist;

import ohos.agp.components.AttrSet;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import com.xuexiang.xui_lib.ResourceTable;
import com.xuexiang.xui_lib.util.Utils;

/**
 * 用作通用列表 {@link XUIGroupListView} 里每个 {@link XUIGroupListView.Section} 的头部或尾部，也可单独使用。
 *
 * @author xuexiang
 * @since 2019/1/3 上午10:48
 */
public class XUIGroupListSectionHeaderFooterView extends DirectionalLayout {

    private Text mText;

    public XUIGroupListSectionHeaderFooterView(Context context) {
        this(context, null, null);
    }

    public XUIGroupListSectionHeaderFooterView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    public XUIGroupListSectionHeaderFooterView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public XUIGroupListSectionHeaderFooterView(Context context, String titleText) {
        this(context);
        setText(titleText);
    }

    public XUIGroupListSectionHeaderFooterView(Context context, String titleText, boolean isFooter) {
        this(context);
        if (isFooter) {
            // Footer View 不需要 padding bottom
            setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), 0);
        }
        setText(titleText);
    }

    private void init(Context context) {
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_xui_layout_group_list_section, this, true);
        setAlignment(LayoutAlignment.BOTTOM);

        mText = (Text) findComponentById(ResourceTable.Id_group_list_section_header_textView);
    }

    public void setText(String text) {
        if (Utils.isNullOrEmpty(text)) {
            mText.setVisibility(HIDE);
        } else {
            mText.setVisibility(VISIBLE);
        }
        mText.setText(text);
    }

    public Text getTextView() {
        return mText;
    }

    public void setTextGravity(int gravity) {
        mText.setTextAlignment(gravity);
    }
}
